package homework.homework04.entry;

import javax.swing.*;
import java.util.Random;
import java.util.function.Supplier;

public class Entry {

    // 验证码可选字符
    private static final String OPTIONS_CAHS = "023456789abcdefghjklmnopqrstuvwxyz" ;
    //创建随机数对象
    private static Random random =new Random();

    public static void main(String[] args) {

        // 在等号的右边补全代码
       /* String verificationCode = verificationCode(new Supplier<String>() {
            @Override
            public String get() {
                //生成验证码的逻辑
                //1、定义一个字符串,用于拼接验证码
                String code = "";
                //生成思维随机索引
                for (int i = 0; i < 4; i++) {
                    char c = OPTIONS_CAHS.charAt(random.nextInt(OPTIONS_CAHS.length()));
                    code += c;
                }
                return code;
            }
        });*/
        //利用Lambda进行简化
        String verificationCode = verificationCode(()-> {
                //生成验证码的逻辑
                //1、定义一个字符串,用于拼接验证码
                String code = "";
                //生成思维随机索引
                for (int i = 0; i < 4; i++) {
                    char c = OPTIONS_CAHS.charAt(random.nextInt(OPTIONS_CAHS.length()));
                    code += c;
                }
                return code;});

        // 输出
        System.out.println("本次生成的验证码为：" + verificationCode);

    }

    // 调用该方法生成一个4位的随机验证码
    public static String verificationCode(Supplier<String> supplier) {
        return supplier.get() ;//典型的对象回调方法
    }

}
